﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Planer.Model;

namespace Planer.UI.Forms
{
    public partial class AddIncomeOutcomeForm : Form
    {
        protected ActionType actionType;
        protected billing bill;

        public AddIncomeOutcomeForm()
        {
            InitializeComponent();
        }

        public AddIncomeOutcomeForm(ActionType ActionType, billing Bill)
        {
            this.actionType = ActionType;
            InitializeComponent();
            CustomInitializeComponent(null);
            bill = Bill;
        }

        protected List<action_category> categories = null;
        private bool added = false;
        protected virtual void CustomInitializeComponent(action_category act)
        {
            comboBoxActionCategory.Items.Clear();
            categories = ActionCategoryModel.GetInstance().GetActionCategoryList();
            if (categories.Count == 0)
            {
                buttonOK.Enabled = false;
                return;
            }
            buttonOK.Enabled = true;

            comboBoxActionCategory.Items.AddRange(categories.ToArray());
            if (act == null)
                comboBoxActionCategory.SelectedIndex = 0;
            else
                comboBoxActionCategory.SelectedItem = act;

            if(!added)
            if (actionType == ActionType.INCOME)
            {
                label1.Text += " дохода";
                Text += "дохода";
            }
            else
            {
                label1.Text += " расхода";
                Text += "расхода";
            }
        }

        private void buttonAddActionCategory_Click(object sender, EventArgs e)
        {
            string sInput = Microsoft.VisualBasic.Interaction.InputBox("Введите название для категории", "Категория");
            if (sInput == "") return;

            action_category cat = new action_category() { name = sInput };
            ModelResult mr = ActionCategoryModel.GetInstance().SaveOrUpdateActionCategory(cat);
            if (mr.Status == OperationStatusType.ERROR)
            {
                MessageBoxUtils.ShowErrorBox(mr.ShowAllMessages);
                return;
            }
            added = true;
            CustomInitializeComponent(cat);
        }

        protected virtual void buttonOK_Click(object sender, EventArgs e)
        {
            int selected = comboBoxActionCategory.SelectedIndex;
            if (selected < 0) return;

            string summ = textBoxSumm.Text.Trim().Replace('.', ',');
            float money = 0;
            try
            {
                money = Convert.ToSingle(summ);
            }
            catch (Exception ex)
            {
                MessageBoxUtils.ShowErrorBox("Некорректно введена сумма");
                return;
            }

            if (actionType == ActionType.OUTCOME)
            {
                money = -money;
            }

            action act = new action()
            {
                billing = bill,
                amount = money,
                action_category = (action_category)comboBoxActionCategory.SelectedItem,
                descr = textBoxDescription.Text,
                create_date = DateTime.Now,
                period = PeriodModel.GetInstance().GetCurrentPeriod()
            };

            ModelResult mr = BillingModel.GetInstance().MoneyOperation(bill, act);
            if (mr.Status == OperationStatusType.ERROR)
            {
                MessageBoxUtils.ShowErrorBox(mr.ShowAllMessages);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
    }
}
